package org.linlinjava.litemall.wx.task;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.linlinjava.litemall.core.system.SystemConfig;
import org.linlinjava.litemall.core.task.Task;
import org.linlinjava.litemall.core.util.BeanUtil;
import org.linlinjava.litemall.db.domain.LitemallGoods;
import org.linlinjava.litemall.db.domain.LitemallOrder;
import org.linlinjava.litemall.db.domain.LitemallOrderGoods;
import org.linlinjava.litemall.db.service.GoodsService;
import org.linlinjava.litemall.db.service.OrderGoodsService;
import org.linlinjava.litemall.db.service.OrderService;
import org.linlinjava.litemall.db.util.DateUtil;
import org.linlinjava.litemall.db.util.OrderUtil;
import org.linlinjava.litemall.wx.service.WxOrderService;

import java.time.LocalDateTime;
import java.util.List;

public class OrderUnpaidTask extends Task {
    private final Log logger = LogFactory.getLog(OrderUnpaidTask.class);
    private int orderId = -1;

    public OrderUnpaidTask(Integer orderId, long delayInMilliseconds){
        super("OrderUnpaidTask-" + orderId, delayInMilliseconds);
        this.orderId = orderId;
    }

    public OrderUnpaidTask(Integer orderId){
        super("OrderUnpaidTask-" + orderId, SystemConfig.getOrderUnpaid() * 60 * 1000);
        this.orderId = orderId;
    }

    @Override
    public void run() {
        logger.info("系统开始处理延时任务---订单超时未付款---" + this.orderId);

        OrderService orderService = BeanUtil.getBean(OrderService.class);
        OrderGoodsService orderGoodsService = BeanUtil.getBean(OrderGoodsService.class);
        GoodsService goodsService = BeanUtil.getBean(GoodsService.class);

        LitemallOrder order = orderService.findById(this.orderId);
        if(order == null){
            return;
        }
        if(!OrderUtil.isCreateStatus(order)){
            return;
        }

        // 设置订单已取消状态
        order.setOrderStatus(OrderUtil.STATUS_CANCEL);
        order.setEndTime(LocalDateTime.now());
        if (orderService.updateWithOptimisticLocker(order) == 0) {
            throw new RuntimeException("更新数据已失效");
        }

        // 商品货品数量增加
        Integer orderId = order.getId();
        // 获取订单是否在秒杀期间生成，且秒杀是否结束
        String addTime = DateUtil.getDateTimeDisplayString(order.getAddTime());
        List<LitemallOrderGoods> orderGoodsList = orderGoodsService.queryByOid(orderId);
        for (LitemallOrderGoods orderGoods : orderGoodsList) {
            // 秒杀是否结束
            LitemallGoods killTime = goodsService.findByGoodIdBetween(orderGoods.getGoodsId(), DateUtil.currentTime());
            if (null != killTime) {
                // 下单时间是否在秒杀期间
                LitemallGoods orderTime = goodsService.findByGoodIdBetween(orderGoods.getGoodsId(), addTime);
                if (null != orderTime) {
                    // 说明秒杀未结束，且商品下单是在秒杀期间，秒杀库存加1
                    goodsService.addKillStock(orderGoods.getGoodsId(), Short.valueOf("1"));
                }
            }
            Short number = orderGoods.getNumber();
            if (goodsService.addStock(orderGoods.getGoodsId(), number) == 0) {
                throw new RuntimeException("商品货品库存增加失败");
            }
        }

        //返还优惠券
//        wxOrderService.releaseCoupon(orderId);

        logger.info("系统结束处理延时任务---订单超时未付款---" + this.orderId);
    }
}
